package main.java.indi.zyj.sword;

import java.util.Arrays;

public class StockManagement {

    public int findMin(int[] nums) {
        int low = 0;
        int high = nums.length - 1;
        while (low < high) {
            int pivot = low + (high - low) / 2;
            if (nums[pivot] < nums[high]) {
                // 有可能pivot就是最小的，所以不能pivot - 1
                high = pivot;
            } else if (nums[pivot] > nums[high]) {
                low = pivot + 1;
            } else {
                high -= 1;
            }
        }
        return nums[low];
    }

    public int stockManagement(int[] stock) {
        int i = 0, j = stock.length - 1;
        while (i < j) {                     // 不是<=
            int m = (i + j) / 2;
            if (stock[m] > stock[j])
                i = m + 1;
            else if (stock[m] < stock[j])
                j = m;
            else                            // 无法判定时缩小范围
                j--;
        }
        return stock[i];
    }

}
